//
//  BaseViewController.m
//  LimitFree
//
//  Created by lijinghua on 16/2/29.
//  Copyright © 2016年 lijinghua. All rights reserved.
//  职责：定制导航栏
//  导航栏的分类：title   ，分类 ＋ title ＋ 设置   ，后退 + title


#import "BaseViewController.h"
#import "CategoryViewController.h"
#import "SettingViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //根据各个子类中的设置，定制不同的导航栏式样
    [self customNavigationBar:self.navigationStyle];
}

- (void)customNavigationBar:(LFNavigationStyle)style{
    //设置导航栏的背景
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault];
    if (style == LFNavigationBackStyle) {
        //定制返回按钮
        [self customBackNavigationItem];
        //定制titleView
        [self customNavigationTitleView];
    }else if(style == LFNavigationTitleStyle){
        //定制titleView
        [self customNavigationTitleView];
    }else if(style == LFNavigationBothStyle){
        //定制左边按钮
        [self customNavigationLeftItem];
        //定制titleView
        [self customNavigationTitleView];
        //定制右边按钮
        [self customNavigationRightItem];
    }
}

#pragma mark - 定制导航栏
- (void)customBackNavigationItem{
    
    UIButton * button = [LimitFreeUIFactory createButton:CGRectMake(0, 0, 63, 30) title:@"后退" titleColor:[UIColor blackColor]];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonbar_back.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
}

- (void)customNavigationTitleView{
    UILabel *label = [LimitFreeUIFactory createLabel:CGRectMake(0, 0, 100, 40) title:self.title font:[UIFont systemFontOfSize:22] textColor:[UIColor colorWithRed:30/255.0f green:160/255.0f blue:230/255.0f alpha:1]];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
}

//工厂来生成常用的控件
- (void)customNavigationLeftItem{
    UIButton *leftButton = [LimitFreeUIFactory createButton:CGRectMake(0, 0, 44, 30) title:@"分类" titleColor:[UIColor blackColor]];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"buttonbar_action"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleNavLeftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
}

- (void)customNavigationRightItem{
    UIButton *rightButton = [LimitFreeUIFactory createButton:CGRectMake(0, 0, 44, 30) title:@"设置" titleColor:[UIColor blackColor]];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"buttonbar_action"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(handleNavRightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
}

#pragma mark - 导航栏控件事件处理
- (void)goBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)handleNavLeftButtonClick:(UIButton*)button{
//    CategoryViewController *categoryViewController = [[CategoryViewController alloc]init];
//    //把自身作为
//    categoryViewController.delegate = self;
//    categoryViewController.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:categoryViewController animated:YES];
}

- (void)handleNavRightButtonClick:(UIButton*)button{
    SettingViewController  *settingViewController = [[SettingViewController alloc]init];
    [self.navigationController pushViewController:settingViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
